﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Internal;

namespace NLite.Mini.Listener
{
    /// <summary>
    /// 
    /// </summary>
    public class ComponentListenManager:ListenerManager<IComponentListener,ComponentListenStage>,IComponentListener,IComponentListenerManager
    {
        /// <summary>
        /// 
        /// </summary>
        public ComponentListenManager() : base(new SyncList<IComponentListener>()) { }

        /// <summary>
        /// 
        /// </summary>
        public IKernel Kernel { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="listner"></param>
        protected override void OnAfterRegister(IComponentListener listner)
        {
            listner.Init(Kernel);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kernel"></param>
        public virtual void Init(IKernel kernel)
        {
            Kernel = kernel;
            if (kernel != null)
                Init();
        }

        /// <summary>
        /// 
        /// </summary>
        protected virtual void Init() { }

        /// <summary>
        /// 
        /// </summary>
        public ComponentListenStage Type { get; private set; }

       

        #region IComponentListener Members

        void IComponentListener.OnMetadataRegistered(IComponentInfo info)
        {
            ForEach(item => item.OnMetadataRegistered(info));
        }

        void IComponentListener.OnPreCreation(NLite.Mini.Context.IPreCreationContext ctx)
        {
            ForEach(item => item.OnPreCreation(ctx));
        }

        void IComponentListener.OnPostCreation(NLite.Mini.Context.IPostCreationContext ctx)
        {
            ForEach(item => item.OnPostCreation(ctx));
        }

        void IComponentListener.OnInitialization(NLite.Mini.Context.IPostCreationContext ctx)
        {
            ForEach(item => item.OnInitialization(ctx));
        }

        void IComponentListener.OnPostInitialization(NLite.Mini.Context.IPostCreationContext ctx)
        {
            ForEach(item => item.OnPostInitialization(ctx));
        }

        void IComponentListener.OnPreDestroy(IComponentInfo info, object instance)
        {
            ForEach(item => item.OnPreDestroy(info,instance));
        }

        void IComponentListener.OnPostDestroy(IComponentInfo info)
        {
            ForEach(item => item.OnPostDestroy(info));
        }

        #endregion

    }
}
